<template>
        <h2 style="text-align: center;">登录</h2>
        <el-card style="max-width: 480px;margin: 260px auto;" >
            <el-form
                ref="ruleFormRef"
                style="max-width: 600px"
                :model="Info"
                :rules="rules"
                label-width="auto"
                class="demo-ruleForm"
                :size="formSize"
                status-icon
                >
                <el-form-item label="用户名称" prop="Accoun_Name">
                    <el-input v-model="Info.Accoun_Name" />
                </el-form-item>

                <el-form-item label="用户密码" prop="Accoun_Password">
                    <el-input v-model="Info.Accoun_Password" type="password" />
                </el-form-item>

                <el-form-item label="验证码" prop="Accoun_Code">
                    <el-input v-model="Info.Accoun_SCode" style="width: 150px;"/>
                    <el-tag type="success"  @click="SetCode">{{ Info.Accoun_KCode}}</el-tag> 
                </el-form-item>

                <el-form-item label="记住密码" prop="Accoun_Code">
                    <el-checkbox v-model="remembed"></el-checkbox>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">
                    登录
                    </el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
</template>
  
<script lang="ts" setup>
    import { reactive, ref ,onMounted} from 'vue'
    import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
    import axios from 'axios'
    import { ElMessage } from 'element-plus'
    import { useRouter } from 'vue-router' 
    //import { useCounterStore } from '@/stores/counter'
    import http from '@/http/AxiosHelper'

    //const store=useCounterStore();
    const router=useRouter();

    const formSize = ref<ComponentSize>('default')
    const ruleFormRef = ref<FormInstance>()
    const Info:any = ref({
        Accoun_Name: "",
        Accoun_Password: "",
        Accoun_SCode:"",
        Accoun_KCode:"",
        Accoun_Answer:"",
    })
    const remembed=ref(false);
    onMounted(()=>{
        SetCode();
        InitChecked();
    })
    //初始化数据
    const InitChecked=()=>{
        const jizhu=localStorage.getItem("remembed")||"";
        if(jizhu)
        {
            Info.value.Accoun_Name=localStorage.getItem("Accoun_Name")||"";
            Info.value.Accoun_Password=localStorage.getItem("Accoun_Password")||"";
            remembed.value=true;
        }
    }
    //设置验证码
    const SetCode=()=>{
        let A=Math.ceil(Math.random()*10);
        let B=Math.ceil(Math.random()*10);
        let X=Math.ceil(Math.random()*4);

        if(X==1)
        {
            Info.value.Accoun_KCode=A+"+"+B+"="+"?";
            Info.value.Accoun_Answer=A+B;
        }
        else if(X==2)
        {
            if( B>A)
            {
                var t;
                t=A;
                A=B;
                B=t;
            }
            Info.value.Accoun_KCode=A+"-"+B+"="+"?";
            Info.value.Accoun_Answer=A-B;
        }
        else if(X==3)
        {
            Info.value.Accoun_KCode=A+"x"+B+"="+"?";
            Info.value.Accoun_Answer=A*B;
        }
        else if(X==4)
        {
            A=A*B;
            Info.value.Accoun_KCode=A+"/"+B+"="+"?";
            Info.value.Accoun_Answer=A/B;
        }
        //Info.value.Accoun_KCode=Math.random().toString().substring(2,6)
    }
    
    const locationOptions = ['Home', 'Company', 'School']
    //验证
    const rules = reactive<FormRules>({
        Accoun_Name: [
             { required: true, message: '请输入用户名称！', trigger: 'blur' },
             //{pattern:/^1[3-9]\d{9}$/,message:"手机号格式不正确！",trigger:'blur'}
        ],
        Accoun_Password: [
             { required: true, message: '请输入用户密码！', trigger: 'blur' },
        ],
        
    })
    //按钮
    const submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        //判断验证码
        if(Info.value.Accoun_SCode!=Info.value.Accoun_Answer)
        {
            ElMessage.error("验证码不正确!");
            return;
        }
        await formEl.validate((valid, fields) => {
            if (valid) {
                axios.get("http://localhost:5192/api/Account/Login",
                    {
                        params:{
                            AccountName:Info.value.Accoun_Name,
                            Password:Info.value.Accoun_Password
                        }
                    }
                )
                .then(res=>{
                    if(res.data.Code==200)
                    {
                        ElMessage.success(res.data.Message);
                        //通过pinia存储数据
                        // store.User_Name=res.data.Data.Accoun_Name;
                        // store.Role_Name=res.data.Data.Role_Name;

                        //通过localStorage存储数据
                        localStorage.setItem("Accoun_RealName",res.data.Data.Accoun_RealName);
                        localStorage.setItem("Role_Name",res.data.Data.Role_Name);
                        localStorage.setItem("token",res.data.Data.Token)

                         
                        //记住密码判断
                        if(remembed.value==true)
                        {
                            localStorage.setItem("Accoun_Name",Info.value.Accoun_Name);
                            localStorage.setItem("Accoun_Password",Info.value.Accoun_Password);
                            localStorage.setItem("remembed",'1');
                        }
                        else
                        {
                            localStorage.removeItem("Accoun_Name");
                            localStorage.removeItem("Accoun_Password");
                            localStorage.removeItem("remembed");
                        }
                        //登录成功添加日志
                        axios.post("http://localhost:5019/api/Log/AddLog",{
                            "Log_Content":res.data.Data.Accoun_RealName+"登录成功",
                            "CreateName":res.data.Data.Accoun_RealName
                        })
                        //登录成功跳转到主页面
                        router.push("/Main");
                    }
                    else
                    {
                        ElMessage.error(res.data.Message);
                    }
                })
            } else {
                console.log('error submit!', fields)
            }
        })
    }
    //重置
    const resetForm = (formEl: FormInstance | undefined) => {
        if (!formEl) return
        formEl.resetFields()
    }
    
    const options = Array.from({ length: 10000 }).map((_, idx) => ({
        value: `${idx + 1}`,
        label: `${idx + 1}`,
    }))
</script>
  